--[[
 * Copyright (C) 2015 - 2018 Ricky K. Thomson
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * u should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 --]]
 
-- entities = {}
-- local path = "boxclip/entities/"
-- print("Loading Entities...")


-- local files = love.filesystem.getDirectoryItems(path)

-- -- load all files in entities/ to allow drop-in modules
-- for i, file in ipairs(files) do
-- 	local info = love.filesystem.getInfo(path .. file)
-- 	if info.type == "file" and file ~= "init.lua" then
-- 		local m = file:match("(.+)%.lua")
-- 		print(i .. ". " .. path .. m)
-- 		require(path .. m)
-- 	end
	
-- end

-- return entities

--物体的管理

-- local Platforms = require("boxclip/entities/platforms")
-- local Decals = require("boxclip/entities/decals")

local EntityManger = class("EntityManger")

function EntityManger:ctor()
	local path = "boxclip/entities/"
	local files = love.filesystem.getDirectoryItems(path)
	for i, file in ipairs(files) do
		local info = love.filesystem.getInfo(path .. file)
		if info.type == "file" and file ~= "init.lua" then
			local m = file:match("(.+)%.lua")
			print(i .. ". " .. path .. m)
			require(path .. m)
		end
	end

	self.entities = {}
	self.entity_type_list = {
		"Decals",
		"Props",
		"Platforms",
		"Springs",
		"Bumpers",
		"Checkpoints",
		"Crates",
		"Portals",
		"Coins",
		"Pickups",
		"Enemies",
		"Traps",
		"Materials",
		"Tips",		

	}

end

--获取一个物体
function EntityManger:getEntity(entity_name)

	-- if entity_name == "Platforms" then 
	-- 	if self.entities[entity_name] then 
	-- 		return self.entities[entity_name]
	-- 	end
	-- 	self.entities[entity_name] = Platforms.new()
	-- end
	-- if entity_name == "Decals" then 
	-- 	if self.entities[entity_name] then 
	-- 		return self.entities[entity_name]
	-- 	end
	-- 	self.entities[entity_name] = Decals.new()
	-- end

	for k, v in pairs(self.entity_type_list) do 
		if entity_name == v then 
			if self.entities[entity_name] then 
				return self.entities[entity_name]
			end
			-- print("__________entity__",v)
			local Entity = require("boxclip/entities/"..v)			
			self.entities[entity_name] = Entity.new()
		end
	end

	return self.entities[entity_name]
end


function EntityManger:update(dt)
	-- platforms:update(dt)
	-- decals:update(dt)
	-- materials:update(dt)
	-- portals:update(dt)
	-- checkpoints:update(dt)
	-- springs:update(dt)
	-- coins:update(dt)
	-- pickups:update(dt)
	-- bumpers:update(dt)
	-- enemies:update(dt)
	-- tips:update(dt)

	local entity = nil
	for k, v in pairs(self.entity_type_list) do 
		entity = self:getEntity(v)
		if entity then 
			entity:update(dt)
		end
	end
end


function EntityManger:draw()

	-- decals:draw() --布瀑
	-- props:draw() --道具
	-- platforms:draw()
	local entity = nil
	for k, v in pairs(self.entity_type_list) do 
		entity = self:getEntity(v)
		if entity then 
			entity:draw()
		end
	end
	-- springs:draw()
	-- bumpers:draw()
	-- checkpoints:draw()
	-- crates:draw()
	-- portals:draw()
	-- coins:draw()
	-- pickups:draw()
	-- enemies:draw()
	-- traps:draw()
	-- materials:draw()
	-- tips:draw()


end




return EntityManger